package com.gimi.cloud.jimi.commonService.common.impl;

import com.aliyuncs.exceptions.ClientException;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.commonService.common.CommonCoreNoticeService;
import com.gimi.cloud.jimi.commonService.common.JmPhoneMsgService;
import com.gimi.cloud.jimi.dao.dao.core.MessageDao;
import com.gimi.cloud.jimi.dto.sys.AlidayuDTO;
import com.gimi.cloud.jimi.entity.core.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service(value = "commonCoreNoticeService")
@Transactional
public class CommonCoreNoticeServiceImpl implements CommonCoreNoticeService {
	private final Logger logger = LoggerFactory.getLogger(CommonCoreNoticeServiceImpl.class);

	private final JmPhoneMsgService jmPhoneMsgService;

	public CommonCoreNoticeServiceImpl(JmPhoneMsgService jmPhoneMsgService){
		this.jmPhoneMsgService = jmPhoneMsgService;
	}

	@Autowired
	private MessageDao messageDao;

	@Override
	public ResponseCodeEnum sendSmsByAliDaYu(Message message, AlidayuDTO dto) throws ClientException {
		String mobile = message.getMobile();
		logger.info("mobile=========" + mobile);

		String result = jmPhoneMsgService.sendMsgByAliYun(dto);
		StringBuilder sb = new StringBuilder();
		if (result.equalsIgnoreCase("ok")) {
			message.setStatus(1);
			sb.append("发送短信成功。");
			logger.info("===> " + sb.toString());
			message.setResult(sb.toString());
			messageDao.insert(message);
			return ResponseCodeEnum.VALIDE_CODE_SEND_SUCCESS;
		} else {
			message.setStatus(0);
			sb.append("发送短信失败：" + result + "。");
			logger.error("===> " + sb.toString());
			message.setResult(sb.toString());
			messageDao.insert(message);
			return ResponseCodeEnum.ACCESS_FAIL_ERR;
		}

	}

}
